package com.young.testcoroutine

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val handler = CoroutineExceptionHandler { _, throwable ->
            Log.d("young_test", "还好有我，不然 app 就抛 $throwable 异常闪退了")
        }

        findViewById<Button>(R.id.button).setOnClickListener {
            GlobalScope.launch {
                Log.d("young_test", "呀，你居然点击了神奇的按钮")
                throw IllegalArgumentException("来个异常试试 App 的健壮性")
            }
        }
    }
}